/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.system;

import org.lwjgl.opengl.Display;

/**
 * @author Daniele Paggi
 * 
 */
public class ASincroD extends Thread {

	/** reference to the Game Core */
	// inutile?
	private Core core;

	/** create a new instance of Sincro */
	public ASincroD() {
		super();
		core = StaticRef.core;
		start();
	}

	/** main loop of game */
	public void run() {
		last = System.currentTimeMillis();
//		CurrentTime = LastTime = System.currentTimeMillis();
		ElaspedTime = 0;
		core.init(); // creation
		while (ASincroL.loop) {
			if (!Display.isCloseRequested()) { // chiude all'ALT + F4
				core.coreRender();
				Display.sync(ASincroL.fps);
				Display.update();
				fps();
			} else
				core.endGame();
		}
		core.cleanup();
	}

	/** used for the fps */
	private static int cont = 0, lastFps = 100;

	/** last time: used for calculate the fps */
	private long last;

	/** calculate the fps */
	private void fps() {
		cont++;
		if (System.currentTimeMillis() - last >= 1000) {
			lastFps = cont;
			System.out.println("FPS: " + cont);
			cont = 0;
			last = System.currentTimeMillis();
		}
	}

	/** returns how much FPS has got the Game in the last cycle */
	public static int getFps() {
		return lastFps;
	}

//	private long LastTime, CurrentTime, ElaspedTime;
	private long ElaspedTime;

	/** returns the elasped time */
	public long getElaspedTime() {
		return ElaspedTime;
	}

}
